import {Injectable} from '@angular/core';
import {ToolsService} from "@shared/service/tools/tools.service";
import {Router, Routes, Route} from "@angular/router";

declare var window;

@Injectable()
export class StatisticsBaiduService {
  private _hmt = [];
  private _window: any;

  constructor(
    private router: Router,
    private tools: ToolsService
  ) {
    this._window = this.tools.nativeWindow;
  }

  buildBasic() {
    const that = this;
    that._hmt = that._hmt || [];
    const hm = document.createElement("script");
    // hm.src = "https://hm.baidu.com/hm.js?c961bb6ba1a0ceb40f0eecebf93ce4c4"; // m.oooseed.com 统计
    hm.src = "https://hm.baidu.com/hm.js?f25468e31a85e05f31fa6d32bb11aef2"; // oooseed.com 统计
    const s = document.getElementsByTagName("script")[0];
    const hm2 = document.createElement("script");
    hm2.src = 'https://hm.baidu.com/hm.js?d6b0d48c1e16fccecf1cf9e92dac1cf3'; // 13322ty.com统计
    s.parentNode.insertBefore(hm, s);
    s.parentNode.insertBefore(hm2, s);
    hm.onload = function () {
      that._hmt = that._window._hmt;
      that.router.config.forEach((route: Route, index) => {
        that._hmt.push(['_trackPageview', route.path]);
      });
    };
    hm2.onload = function () {
      that._hmt = that._window._hmt;
      that.router.config.forEach((route: Route, index) => {
        that._hmt.push(['_trackPageview', route.path]);
      });
    };
  }

}
